#include "ghost.h"
#include <QTime>
#include <QDebug>
#include <QFontDatabase>

using namespace Qt;
//------------------------------------//
// Ghost
//------------------------------------//

Ghost::Ghost(
    QObject* parent,
    QString path, QString name, int id, GhostColor color,
    BaneSkill skill_l, BaneSkill skill_m, BaneSkill skill_r,
    int resist0, int resist1, int resist2, int resist3, int resist4,
    bool movable, bool boss, bool buddha
    )
    : QObject(parent),
      _imgpath(path), _ghostname(name), _ghostid(id),_color(color),
      _skill_l(skill_l), _skill_m(skill_m), _skill_r(skill_r),
      _resist0(resist0), _resist1(resist1),
      _resist2(resist2), _resist3(resist3),_resist4(resist4),
      _movable(movable), _boss(boss), _buddha(buddha)

{
    //nothing atm.
}

QString Ghost::ColorToString( GhostColor c )
{
    switch( c )
    {
        case RED:
            return tr("red");
        case YELLOW:
            return tr("yellow");
        case GREEN:
            return tr("green");
        case BLUE:
            return tr("blue");
        case BLACK:
            return tr("black");
        case WHITE:
            return tr("white");
    }
    return tr("Not defined");
}
Qt::GlobalColor Ghost::ColorToQColor( GhostColor c )
{
    switch( c )
    {
        case RED:
            return Qt::red;
        case YELLOW:
            return Qt::yellow;
        case GREEN:
            return Qt::green;
        case BLUE:
            return Qt::blue;
        case BLACK:
            return Qt::black;
        case WHITE:
            return Qt::white;
    }
    return Qt::white;
}

QString Ghost::SkillToString( BaneSkill s )
{
    switch( s )
    {
        case NOTHING:
            return tr("Nothing");
        case ONE_MORE_CARD:
            return tr("One more card");
        case INSTANT_HAUNT:
            return tr("Instant haunt");
        case CURSE_DICE:
            return tr("Curse dice");
        case LOSE_QI:
            return tr("Lose one Qi");
        case GHOSTS_MARCH:
            return tr("Ghost march");
        case SEAL_SKILLS:
            return tr("Seal skills");
        case DICE_HOLDER:
            return tr("Dice holder");
        case QI_PLUS_YINYANG:
            return tr("Qi + Yinyang");
        case HAUNTER:
            return tr("Haunter");
        case TAO:
            return tr("One Tao token");
    }
    return tr("Not defined");
}

//------------------------------------//
// GhostManager
//------------------------------------//

GhostManager::GhostManager( QObject* parent )
    : QObject( parent )
{
    _ghostcount = 0;
}

void GhostManager::add( Ghost* ghost )
{
    if( ghost->isBoss() )
        _bglist << ghost ;
    else
        _tglist << ghost;
    _ghostcount ++;
}


// get a deck of cards for playing.
// The pattern of ghost deck is:
//   _trash_ trash cards, then 1 boss card, repeat for _rounds_ rounds
//
// shuffle the lists if it is true
QList <Ghost*> GhostManager::getDeck( int rounds, int trash, bool shuffle )
{
    qsrand( QTime::currentTime().msec() );
    QList <Ghost*> res;
    if( shuffle )
    {
        int k, ts = 0, bs = 0;
        for( int i = 0; i < rounds; i ++ )
        {
            for( int j = 0; j < trash; j ++ )
            {
                // random a trash ghost from trash list
                // it should have not appeared in the list yet
                while( ts < _tglist.count() )
                {
                    k = qrand() % _tglist.count();
                    if( !res.contains( _tglist.at( k ) ) )
                    {
                        res << _tglist.at( k ) ;
                        ts ++;
                        break;
                    }
                }
            }
            // random a boss ghost from trash list
            // it should have not appeared in the list yet
            while( bs < _bglist.count() )
            {
                k = qrand() % _bglist.count();
                if( !res.contains( _bglist.at( k ) ) )
                {
                    res << _bglist.at( k ) ;
                    bs ++;
                    break;
                }
            }
            // if there are still trashes left.
            while( ts < _tglist.count() )
            {
                k = qrand() % _tglist.count();
                if( !res.contains( _tglist.at( k ) ) )
                {
                    res << _tglist.at( k ) ;
                    ts ++;
                }
            }
        }
    }
    else
    {
        for( int i = 0; i < rounds; i ++ )
        {
            for( int j = 0; j < trash; j ++ )
            {
                res << _tglist.at( i * trash + j );
            }
            res << _bglist.at( i );
        }
    }
    return res;
}

/* return a deck of ghost cards for testing purpose */
QList <Ghost*> GhostManager::TestDeck()
{
    Ghost *ghost0 = new Ghost( 0, tr(":/common/images/bghost0.jpg"),
                              tr("Girl"), 0, Ghost::BLACK,
                              Ghost::ONE_MORE_CARD, Ghost::CURSE_DICE, Ghost::INSTANT_HAUNT,
                              3, 2, 3, 0, 0, false, true, true );
    Ghost *ghost1 = new Ghost( 0, tr(":/common/images/tghost0.jpg"),
                              tr("Zhang San"), 1, Ghost::RED,
                              Ghost::LOSE_QI, Ghost::HAUNTER, Ghost::NOTHING,
                              0, 1, 3, 0, 0, true, false );
    Ghost *ghost2 = new Ghost( 0, tr(":/common/images/tghost1.jpg"),
                              tr("Li Si"), 2, Ghost::BLUE,
                              Ghost::NOTHING, Ghost::CURSE_DICE, Ghost::NOTHING,
                              1, 0, 0, 0, 0, true, false );
    Ghost *ghost3 = new Ghost( 0, tr(":/common/images/tghost2.jpg"),
                              tr("Chun Ge"), 3, Ghost::BLUE,
                              Ghost::INSTANT_HAUNT, Ghost::NOTHING, Ghost::TAO,
                              0, 0, 2, 0, 0, true, false );
    Ghost *ghost4 = new Ghost( 0, tr(":/common/images/tghost3.jpg"),
                              tr("Cao Nima"), 4, Ghost::BLUE,
                              Ghost::NOTHING, Ghost::HAUNTER, Ghost::TAO,
                              0, 0, 0, 0, 3, true, false );
    Ghost *ghost5 = new Ghost( 0, tr(":/common/images/tghost4.jpg"),
                              tr("Spring Bro."), 5, Ghost::BLUE,
                              Ghost::ONE_MORE_CARD, Ghost::NOTHING, Ghost::TAO,
                              0, 0, 0, 4, 0, true, false );
    Ghost *ghost6 = new Ghost( 0, tr(":/common/images/bghost1.jpg"),
                              tr("Mage"), 6, Ghost::RED,
                              Ghost::DICE_HOLDER, Ghost::HAUNTER, Ghost::QI_PLUS_YINYANG,
                              0, 0, 2, 0, 2, false, true, true );
    GhostManager gm;

    gm.add( ghost0 );
    gm.add( ghost1 );
    gm.add( ghost2 );
    gm.add( ghost3 );
    gm.add( ghost4 );
    gm.add( ghost5 );
    gm.add( ghost6 );
    return gm.getDeck( 2, 2 );
}


Ghost *GhostManager::DefaultGhost()
{
    Ghost *res = new Ghost( 0, tr(":/common/images/unknown.png"),
                            tr("N/A"), -1, Ghost::WHITE,
                            Ghost::NOTHING, Ghost::NOTHING, Ghost::NOTHING );
    return res;
}


/*

    Resist item

*/


ResistItem::ResistItem(QGraphicsObject *parent)
    :QGraphicsObject( parent )
{
    this->setOpacity( 0 );
    _null = true;
    _anim_0 = new QPropertyAnimation( this, "opacity" );
    _anim_0->setDuration( 100 );
    _anim_0->setEndValue( 0.5 );

    _anim_1 = new QPropertyAnimation( this, "opacity" );
    _anim_1->setDuration( 100 );
    _anim_1->setEndValue( 1 );

    _anim_group_0 = new QSequentialAnimationGroup( this );
    _anim_group_0->addAnimation( _anim_0 );
    _anim_group_0->addAnimation( _anim_1 );

    _anim_2 = new QPropertyAnimation( this, "scale" );
    _anim_2->setDuration( 300 );
    _anim_2->setStartValue( 2 );
    _anim_2->setEndValue( 1 );

    _anim_group_1 = new QParallelAnimationGroup( this );
    _anim_group_1->addAnimation( _anim_2 );
    _anim_group_1->addAnimation( _anim_group_0 );

    connect( _anim_0, SIGNAL( finished() ), this, SLOT( half_slot() ) );
}
void ResistItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    if( !_null )
    {
        painter->drawPixmap( -_w/2, -_h/2, _w, _h, _qp );
    }
}


/*******************

ResistGraphicsObjects

********************/

ResistItemGroup::ResistItemGroup( QGraphicsObject *parent, Ghost* ghost )
: QGraphicsObject( parent ),
  _resist0( ghost->getResist0() ),
  _resist1( ghost->getResist1() ), _resist2( ghost->getResist2() ),
  _resist3( ghost->getResist3() ), _resist4( ghost->getResist4() )
{
    _total_resist = _resist0 + _resist1 + _resist2 + _resist3 + _resist4;
    _qp0 = QPixmap( tr(":/resist/images/resist_0.png") );
    _qp1 = QPixmap( tr(":/resist/images/resist_1.png") );
    _qp2 = QPixmap( tr(":/resist/images/resist_2.png") );
    _qp3 = QPixmap( tr(":/resist/images/resist_3.png") );
    _qp4 = QPixmap( tr(":/resist/images/resist_4.png") );

    for( int i = 0; i < 10; i ++ )
    {
        _items << new ResistItem( this );
    }
    for( int i = 0; i < 9; i ++ )
    {
        /* set up the animation chain */
        connect( _items.at( i ), SIGNAL( half_finished() ),
                 _items.at( i + 1 ), SLOT( animate() ) );
    }
}

void ResistItemGroup::setGhost( Ghost* ghost )
{
    _resist0 = ghost->getResist0();
    _resist1 = ghost->getResist1();
    _resist2 = ghost->getResist2();
    _resist3 = ghost->getResist3();
    _resist4 = ghost->getResist4();
    _total_resist = _resist0 + _resist1 + _resist2 + _resist3 + _resist4;

    int i, count = 0;
    int hoffset = - _total_resist * ( _qp0.width() + _hgap ) / 2;
    int voffset = -_qp0.height() / 2;

    for( i = 0; i < _resist0; i ++ )
    {
        _items.at( count )->setResistance( _qp0 );
        _items.at( count )->setPos( hoffset, voffset );
        _items.at( count )->setOpacity( 0 );
        hoffset += _qp0.width() + _hgap;
        count ++;
    }
    for( i = 0; i < _resist1; i ++ )
    {
        _items.at( count )->setResistance( _qp1 );
        _items.at( count )->setPos( hoffset, voffset );
        _items.at( count )->setOpacity( 0 );
        hoffset += _qp0.width() + _hgap;
        count ++;
    }
    for( i = 0; i < _resist2; i ++ )
    {
        _items.at( count )->setResistance( _qp2 );
        _items.at( count )->setPos( hoffset, voffset );
        _items.at( count )->setOpacity( 0 );
        hoffset += _qp0.width() + _hgap;
        count ++;
    }
    for( i = 0; i < _resist3; i ++ )
    {
        _items.at( count )->setResistance( _qp3 );
        _items.at( count )->setPos( hoffset, voffset );
        _items.at( count )->setOpacity( 0 );
        hoffset += _qp0.width() + _hgap;
        count ++;
    }
    for( i = 0; i < _resist4; i ++ )
    {
        _items.at( count )->setResistance( _qp4 );
        _items.at( count )->setPos( hoffset, voffset );
        _items.at( count )->setOpacity( 0 );
        hoffset += _qp0.width() + _hgap;
        count ++;
    }

    for( ; count < _items.size(); count ++ )
    {
        _items.at( count )->setNull();
    }

    _items.at( 0 )->animate();
}

QRectF ResistItemGroup::boundingRect() const
{
    int w = 220;
    int h = 40;
    return QRectF( -w/2, -h/2, w, h );
}

void ResistItemGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
}

void ResistItemGroup::updateState()
{
    update();
}

/*******************

GhostGraphicsInfo

********************/

GhostInfo::GhostInfo( QGraphicsObject *parent )
    : QGraphicsObject( parent ), _clickcount( 0 )
{
    _bg         = QPixmap( ":/common/images/ginfo_bg.png" );
    INFO_WIDTH  = _bg.width();
    INFO_HEIGHT = _bg.height();

    _dghost     = GhostManager::DefaultGhost();
    _ghost      = _dghost;
    _skillitem  = new GhostSkillSlotGroup( this );
    _carditem   = new GhostCard( this );
    _resistitem = new ResistItemGroup( this, _ghost );

    QFontDatabase::addApplicationFont( tr(":/font/font/CHILLER.TTF") );
    _font = QFont( "Chiller" );
    _font.setPixelSize( 40 );
    _font.setBold( true );

    _skillitem->setPos( 0,  200 );
    _resistitem->setPos( 10, 125 );
    _carditem->setPos( 0,  - 35 );

    _carditem->setGhost(   _ghost );
    _resistitem->setGhost( _ghost );
    _skillitem->setGhost(  _ghost );

    /* Set up fade in/out animation for the card item */
    _anim_card0 = new QPropertyAnimation( _carditem, "opacity");
    _anim_card0->setDuration( 100 );
    _anim_card0->setEndValue( 0.0 );

    _anim_card1 = new QPropertyAnimation( _carditem, "opacity");
    _anim_card1->setDuration( 100 );
    _anim_card1->setEndValue( 1 );


    /* Card fade out animation */
    connect( this, SIGNAL( ghostChanged() ),
             _anim_card0, SLOT( start() ) );
    /* Card fade in animation */
    connect( _anim_card0, SIGNAL( finished() ),
             this, SLOT( fadeFinished() ) );

    this->setGhost( _ghost );
}

QRectF GhostInfo::boundingRect() const
{
    return QRectF( -INFO_WIDTH/2, -INFO_HEIGHT/2, INFO_WIDTH, INFO_HEIGHT );
}

Ghost* GhostInfo::currentGhost()
{
    return _ghost;
}

void GhostInfo::fadeFinished()
{
    _anim_card1->start();
    _carditem->setGhost( _ghost );
}

void GhostInfo::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    painter->setFont( _font );
    painter->drawPixmap( -INFO_WIDTH/2, -INFO_HEIGHT/2, INFO_WIDTH, INFO_HEIGHT, _bg );
    painter->drawText( -2 - INFO_WIDTH/2, -INFO_HEIGHT/2 + 13, 220, 50, Qt::AlignCenter, _ghost->getName() );
    QPen pen;
    pen.setColor( white );
    painter->setPen( pen );
    painter->drawText( - INFO_WIDTH/2, -INFO_HEIGHT/2 + 15, 220, 50, Qt::AlignCenter, _ghost->getName() );
}

void GhostInfo::setGhost( Ghost *ghost )
{
    if( ghost == 0 )
    {
        _ghost = _dghost;
    }
    else
    {
        _ghost = ghost;
    }
    _skillitem->setGhost( _ghost );
    _resistitem->setGhost( _ghost );
    _resistitem->updateState();
    emit ghostChanged();
    update();
}

/* GhostDeck */
GhostDeck::GhostDeck( QGraphicsObject *parent, GhostInfo* ginfo, PlayerTable *ptable )
    :GhostInfo( parent ), _ginfo( ginfo ), _ptable( ptable )
{
    _fliped = false;
    this->setAcceptHoverEvents( true );
    newDeck();
    connect( _carditem, SIGNAL( clicked() ),
             this, SLOT( clicked() ));
    connect( _carditem, SIGNAL( hover_entered() ),
             this, SLOT( hover_in() ));
    connect( _carditem, SIGNAL( hover_left() ),
             this, SLOT( hover_out() ));
}

void GhostDeck::clicked()
{
    GhostSlot *slot;
    if( _testdeck.size() == 0 )
        return; // should not be empty
    if( !_fliped )
    {
        flip();
        slot = _ptable->askForSlot();
        slot->setGhost( pop() );
    }
}

void GhostDeck::hover_in()
{
    _ginfo->setGhost( this->currentGhost() );
}
void GhostDeck::hover_out()
{
    _ginfo->setGhost( 0 );
}

void GhostDeck::flip()
{
    this->setGhost( _testdeck.first() );
    _fliped = true;

    if( _ghost->isBoss() )
        emit bossInvoked();
}
Ghost* GhostDeck::pop()
{
    this->setGhost( _dghost );
    _fliped = false;
    if( _testdeck.size() == 1 )
        _carditem->setVisible( false );
    return _testdeck.takeFirst();
}
void GhostDeck::newDeck()
{
    _testdeck << GhostManager::TestDeck();
}

/*

    GhostCardGraphicsObject

*/
GhostCard::GhostCard(QGraphicsObject *parent )
    :QGraphicsObject( parent )
{
    _scale  = 0.8;

    _qpbg   = QPixmap( ":/common/images/ghost_card_bg.png" );
    _qpcard = QPixmap( ":/common/images/unknown.png" );

    _color  = Qt::white;

    _width  = _qpbg.width()  * _scale;
    _height = _qpbg.height() * _scale;
    _cwidth = _qpcard.width()* _scale;
    _cheight= _qpcard.height()*_scale;

    _rib0   = QPixmap( ":/common/images/rib_r0.png" );
    _rib1   = QPixmap( ":/common/images/rib_r1.png" );
    _rib2   = QPixmap( ":/common/images/rib_r2.png" );
    _rib3   = QPixmap( ":/common/images/rib_r3.png" );
    _rib4   = QPixmap( ":/common/images/rib_r4.png" );
    _qpboss = QPixmap( ":/common/images/boss_icon.png" );
    setAcceptHoverEvents( true );
}

QRectF GhostCard::boundingRect() const
{
    return QRectF( -_width/2 - 10, -_height/2 - 10,
                   _width + 20, _height + 20 );
}

void GhostCard::setGhost( Ghost *ghost )
{
    _ghost  = ghost;
    _qpcard = QPixmap( ghost->getImagePath() );
    _color  = Ghost::ColorToQColor( ghost->getColor() );
    update();
}

void GhostCard::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    qreal myscale = 1;
    /*
    bool over = option->state & QStyle::State_MouseOver;
    if( over )
    {
        myscale = 0.98;
    }
    */
    painter->drawPixmap( -_width * myscale / 2, - _height * myscale / 2,
                         _width * myscale, _height * myscale, _qpbg );
    painter->drawPixmap( -_cwidth * myscale / 2, -_cheight * myscale / 2,
                         _cwidth * myscale, _cheight * myscale, _qpcard );
    switch( _color )
    {
        case red:
            painter->drawPixmap( -_width/2, -_height/2, _width, _height, _rib0 );
            break;
        case yellow:
            painter->drawPixmap( -_width/2, -_height/2, _width, _height, _rib1 );
            break;
        case blue:
            painter->drawPixmap( -_width/2, -_height/2, _width, _height, _rib2 );
            break;
        case green:
            painter->drawPixmap( -_width/2, -_height/2, _width, _height, _rib3 );
            break;
        case black:
            painter->drawPixmap( -_width/2, -_height/2, _width, _height, _rib4 );
            break;
        default:
            break;
    }
    if( _ghost->isBoss() )
    {
        painter->drawPixmap( _width/4 + 3, -_height/2 + 23, _qpboss );
    }
}

/*

    SkillSlotGraphicsObject

*/
GhostSkillSlot::GhostSkillSlot( QGraphicsObject* parent )
    :QGraphicsObject( parent ), _bg( QPixmap( ":/common/images/gcard_skill_slot.png" ) )
{
    this->setAcceptHoverEvents( true );
    _w = _bg.width();
    _h = _bg.height();
    _iconw = _w * 0.65;
    _iconh = _h * 0.65;
}

void GhostSkillSlot::setSkill( Ghost::BaneSkill skill )
{
    _skill = skill;
    _qpskill = SkillQPixmap( skill );
    update();
}

QRectF GhostSkillSlot::boundingRect() const
{
    return QRectF( -_w / 2, -_h / 2, _w, _h );
}

void GhostSkillSlot::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    qreal myscale = 1;
    bool over = option->state & QStyle::State_MouseOver;
    if( over )
    {
        myscale = 0.98;
        painter->fillRect( -_iconw/2 , -_iconh/2,
                           _iconw, _iconh, white );
    }
    painter->drawPixmap( -_w/2 * myscale, -_h/2 * myscale, _w * myscale, _h * myscale, _bg );
    painter->drawPixmap( -_iconw/2 * myscale, -_iconh/2 * myscale,
                         _iconw * myscale, _iconh * myscale, _qpskill );

}

QPixmap GhostSkillSlot::SkillQPixmap( Ghost::BaneSkill skill )
{
    switch( skill )
    {
    case Ghost::INSTANT_HAUNT:
        return QPixmap( ":/common/images/skill_instant_haunt.png" );
    case Ghost::ONE_MORE_CARD:
        return QPixmap( ":/common/images/skill_one_more_card.png" );
    case Ghost::CURSE_DICE:
        return QPixmap( ":/common/images/skill_curse.png" );
    case Ghost::SEAL_SKILLS:
        return QPixmap( ":/common/images/skill_seal_skill.png" );
    case Ghost::TAO:
        return QPixmap( ":/common/images/skill_tao.png" );
    case Ghost::LOSE_QI:
        return QPixmap( ":/common/images/skill_lose_qi.png" );
    case Ghost::DICE_HOLDER:
        return QPixmap( ":/common/images/skill_dice_holder.png" );
    case Ghost::QI_PLUS_YINYANG:
        return QPixmap( ":/common/images/skill_yinyang.png" );
    case Ghost::HAUNTER:
        return QPixmap( ":/common/images/skill_haunt.png" );
    default:
        return QPixmap(":/common/images/default_skill.png" );
    }
}

/*

    SkillSlotGraphicsObjects

*/

GhostSkillSlotGroup::GhostSkillSlotGroup( QGraphicsObject *parent )
    : QGraphicsObject( parent )
{
    _skill_slot_l = new GhostSkillSlot( this );
    _skill_slot_m = new GhostSkillSlot( this );
    _skill_slot_r = new GhostSkillSlot( this );

    _w = _skill_slot_l->_w * 2 + HGAP * 2;
    _h = _skill_slot_l->_h;

    _skill_slot_l->setPos( - _skill_slot_l->_w / 2 - HGAP, - _skill_slot_l->_h / 2  );
    _skill_slot_m->setPos( 0, - _skill_slot_l->_h / 2  );
    _skill_slot_r->setPos( _skill_slot_l->_w / 2 + HGAP, - _skill_slot_l->_h / 2  );
}

QRectF GhostSkillSlotGroup::boundingRect() const
{
    return QRectF( -_w / 2, -_h / 2, _w, _h );
}


void GhostSkillSlotGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
}

void GhostSkillSlotGroup::setGhost(Ghost *ghost)
{
    _ghost = ghost;
    _skill_slot_l->setSkill( ghost->getSkillL() );
    _skill_slot_m->setSkill( ghost->getSkillM() );
    _skill_slot_r->setSkill( ghost->getSkillR() );
    update();
}

/*

    class GhostCutIn

*/
GhostCutIn::GhostCutIn( QGraphicsItem *parent, GhostInfo* ghostinfo, InputBlocker* blocker )
    : QGraphicsObject( parent ), _ginfo( ghostinfo )
{

    _bg = QPixmap( ":/common/images/cut_in_bg.png" );
    _qpcard = QPixmap( ":/common/images/unknown.png" );
    _w = _bg.width();
    _h = _bg.height();
    _cw = _qpcard.width();
    _ch = _qpcard.height();

    _anim_group_1 = new GameParAnimation( this );
    _anim_group_2 = new GameParAnimation( this );
    _anim_group_3 = new GameParAnimation( this );

    _anim_group_1->addFade( this, 0, 1, 500 );
    _anim_group_1->addMove( this, QPointF( -_w, 0 ), QPointF( -_w/4, 0 ), 500 );

    _anim_group_2->addMove( this, QPointF( -_w/4, 0 ), QPointF( 0, 0 ), 2500 );

    _anim_group_3->addFade( this, 1, 0, 500 );
    _anim_group_3->addMove( this, QPointF( 0, 0 ), QPointF( _w/8, 0 ), 500 );

    _anim_group_0 = new GameSeqAnimation( this );
    _anim_group_0->addAnimation( _anim_group_1 );
    _anim_group_0->addAnimation( _anim_group_2 );
    _anim_group_0->addAnimation( _anim_group_3 );

    QFontDatabase::addApplicationFont( tr(":/font/font/CHILLER.TTF") );
    _font = QFont( "Chiller" );
    _font.setBold( true );

    this->setOpacity( 0 );

    connect( _ginfo, SIGNAL( bossInvoked() ),
             this, SLOT( animate() ) );

    connect( _ginfo, SIGNAL( bossInvoked() ),
             blocker, SLOT( block() ) );

    connect( this, SIGNAL( cutin_finished() ),
             blocker, SLOT( unblock() ) );
}

QRectF GhostCutIn::boundingRect() const
{
    return QRectF( -1200, -900, 2400, 1800 );
}

void GhostCutIn::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    painter->drawPixmap( -_w/2, -_h/2, _bg );
    painter->drawPixmap( -_cw/2, -_ch/2, _qpcard );

    QPen pen;

    pen.setColor( red );
    painter->setPen( pen );

    _font.setPixelSize( 150 );
    painter->setFont( _font );
    painter->drawText( -_w/2 + this->pos().x() * 1.5 + 550, _h/3,  _ghost->getName() );


    pen.setColor( white );
    painter->setPen( pen );
    painter->drawText( - this->pos().x() * 1.8 - 350, -_h/4,  tr("Boss Arrived!") );
}

void GhostCutIn::animate()
{
    setGhost( _ginfo->currentGhost() );
    connect( _anim_group_0, SIGNAL( finished() ), this, SLOT( animate_finished() ) );
    _anim_group_0->start();
}

void GhostCutIn::animate_finished()
{
    emit cutin_finished();
}

void GhostCutIn::setGhost( Ghost *ghost )
{
    _ghost = ghost;
    _qpcard = QPixmap( ghost->getImagePath() );
}



/*

    class Ghost PIX MAP

*/

void GhostPixmap::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    if(!slotUse)
        painter->drawPixmap(-_pix.width()/2, -_pix.height()/2, _pix);
    else{
        int slotWidth = GhostBoard::BOARD_WIDTH/3;
        int slotHeight = GhostBoard::BOARD_HEIGHT;
        _pix.scaled(QSize(GhostBoard::BOARD_WIDTH/3,
                                GhostBoard::BOARD_HEIGHT), Qt::KeepAspectRatio, Qt::SmoothTransformation);
        painter->drawPixmap(-slotWidth/2,-slotHeight/2,slotWidth, slotHeight, _pix,
                            _pix.width()/2-slotWidth/2, 30, slotWidth, slotHeight);
    }

}

